package com.mpgame.common.mq;

import java.util.Map;
import org.apache.kafka.common.serialization.Deserializer;
import com.google.protobuf.Message;
import com.google.protobuf.Parser;

public class ProtobufDeserializer<T extends Message> implements Deserializer<T> {
    private Parser<? extends Message> parser;
    @Override public void configure(Map<String, ?> configs, boolean isKey) {}
    public ProtobufDeserializer() {}
    public ProtobufDeserializer(Parser<? extends Message> parser) { this.parser = parser; }
    @SuppressWarnings("unchecked")
    @Override public T deserialize(String topic, byte[] data) {
        if (data == null || parser == null) return null;
        try { return (T) parser.parseFrom(data); } catch (Exception e) { return null; }
    }
    @Override public void close() {}
}
